package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppCompatActivity;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils.NetType;
import com.hundsun.yr.universal.library.widget.HSSipEditText;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Config.SPPath;
import com.hundsun.yr.universal.p2p.presenter.impl.PasswordVerifyPresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.ui.view.ProgressDailog;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.PasswordVerifyView;

public class PasswordVerifyDialogActivity extends BaseAppCompatActivity implements PasswordVerifyView, OnClickListener {

	private TextView btnSure;
	private TextView btnCancel;
	private TextView tvLoginName;
	private HSSipEditText passedwordEditText;
	private HSSharedPreferences hsSharedPreferences = null;
	private HSSharedPreferences mConfigSP;
	private PasswordVerifyPresenterImpl mPasswordVerifyPresenter = null;
	private String comefrom;

	/** 加载进度条 **/
	protected ProgressDailog progressDailog = null;
	
	protected void initViewsAndEvents() {

		hsSharedPreferences = new HSSharedPreferences(PasswordVerifyDialogActivity.this, Config.SPPath.file_userLoginedInfo);
		mConfigSP = new HSSharedPreferences(mContext, SPPath.file_configInfo);
		mPasswordVerifyPresenter = new PasswordVerifyPresenterImpl(this, this);
		findView();
		setView();
		
		btnSure.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		passedwordEditText.requestFocus();
		passedwordEditText.setFocusable(false);
		passedwordEditText.setFocusable(true);
	}

	private void findView() {
		btnSure = (TextView) findViewById(R.id.btn_sure);
		btnCancel = (TextView) findViewById(R.id.btn_cancle);
		tvLoginName = (TextView) findViewById(R.id.tv_login_name);
		passedwordEditText =  (HSSipEditText)findViewById(R.id.pet_password);
	}

	/**
	 * 设置各个view的属性
	 */
	private void setView() {
		tvLoginName.setText(CommTool.encodPhone(hsSharedPreferences.getShareProf(UserInfo.MOBILE)));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_cancle:
			finish();
			break;
		case R.id.btn_sure:
			mPasswordVerifyPresenter.validateCredentials(hsSharedPreferences.getShareProf(UserInfo.MOBILE), passedwordEditText.getText().toString());
			break;
		default:
			break;
		}
	}

	@Override
	protected int getContentViewLayoutID() {
		return R.layout.act_verify_password_dialog;
	}

	@Override
	protected void getBundleExtras(Bundle extras) {
		comefrom = extras.getString("comefrom");
	}

	@Override
	protected void onEveComming(@SuppressWarnings("rawtypes") EventCenter eventCenter) {

	}

	@Override
	protected boolean isBindEventBusHere() {
		return false;
	}

	@Override
	protected View getLoadingTargetView() {
		return null;
	}

	@Override
	protected void onNetworkConnected(NetType type) {

	}

	@Override
	protected void onNetworkDisConnected() {

	}

	@Override
	protected boolean isApplyStatusBarTranslucency() {
		return true;
	}

	@Override
	protected boolean toggleOverridePendingTransition() {
		return false;
	}

	@Override
	protected TransitionMode getOverridePendingTransitionMode() {
		return null;
	}

	@Override
	public void showLoading(String msg) {

	}

	@Override
	public void showProgress(String msg) {
		progressDailog = new ProgressDailog(this, R.style.progress_dialog);
		if (TextUtils.isEmpty(msg)) {
			progressDailog.showProgress();
		} else {
			progressDailog.showProgress(msg);
		}
	}

	@Override
	public void closeProgress() {
		if (progressDailog != null && progressDailog.isShowing()) {
			progressDailog.closeProgress();
		}
	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void showError(String msg, OnClickListener onClickListener) {

	}

	@Override
	public void showEmpty(int drawableRes, String msg, OnClickListener onClickListener) {

	}

	@Override
	public void showNetError(OnClickListener onClickListener) {

	}

	@Override
	public void showValidateError(String error) {
		showToast(error);
	}

	@Override
	public void navigateToSetGestureLock() {
		//删除原手势密码
		mConfigSP.putShareProf(hsSharedPreferences.getShareProf(UserInfo.USER_NO)+ Config.GESTURE, null);
		mConfigSP.putBoolean(hsSharedPreferences.getShareProf(UserInfo.USER_NO) + Config.IF_GESTURE, false);
		
		readyGoThenKill(GestureLockActivity.class);

		if (BaseAppManager.getInstance().getActivity(GestureLockLoginActivity.class) != null){
			BaseAppManager.getInstance().getActivity(GestureLockLoginActivity.class).finish();
		}

		if (BaseAppManager.getInstance().getActivity(GestureLockVerifyActivity.class) !=null){
			BaseAppManager.getInstance().getActivity(GestureLockVerifyActivity.class).finish();
		}

		if ("GestureLockVerifyActivity".equals(comefrom)) {
			BaseActivity ac = (BaseActivity) BaseAppManager.getInstance().getActivity(GestureLockVerifyActivity.class);
			ac.finish();
		}
		if ("GestureLockLoginActivity".equals(comefrom)) {
			BaseActivity ac = (BaseActivity) BaseAppManager.getInstance().getActivity(GestureLockLoginActivity.class);
			ac.finish();
		}
	}

}
